﻿using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Linq;
using System.Xml.Serialization;

namespace System.Windows.Forms
{
	[ListBindable(false), System.Xml.Serialization.XmlRoot("Columns"), System.Configuration.SettingsSerializeAs(System.Configuration.SettingsSerializeAs.Xml), Serializable]
	public class ColumnHeaderExCollection : System.Collections.Generic.IList<ColumnHeaderEx>, IList, IXmlSerializable
	{
		private ListView.ColumnHeaderCollection list;
		private ListViewEx listView;

		internal ColumnHeaderExCollection() { }

		internal ColumnHeaderExCollection(ListViewEx listView)
		{
			this.ListView = listView;
		}

		public int Count
		{
			get { return list.Count; }
		}

		internal ListViewEx ListView
		{
			get { return listView; }
			set
			{
				this.listView = value;
				this.list = value.BaseColumns;
			}
		}

		bool ICollection.IsSynchronized
		{
			get { return true; }
		}

		object ICollection.SyncRoot
		{
			get { return this; }
		}

		bool IList.IsFixedSize
		{
			get { return false; }
		}

		public bool IsReadOnly
		{
			get { return false; }
		}

		object IList.this[int index]
		{
			get { return this[index]; }
			set
			{
				throw new NotImplementedException();
			}
		}

		ColumnHeaderEx IList<ColumnHeaderEx>.this[int index]
		{
			get
			{
				return this[index];
			}
			set
			{
				if (index >= list.Count)
					throw new IndexOutOfRangeException();
				list.RemoveAt(index);
				list.Insert(index, value);
			}
		}

		public ColumnHeaderEx this[int index]
		{
			get { return list[index] as ColumnHeaderEx; }
		}

		public ColumnHeaderEx this[string key]
		{
			get { return list[key] as ColumnHeaderEx; }
		}

		public int Add(ColumnHeaderEx item)
		{
			return list.Add(item);
		}

		public ColumnHeaderEx Add(string text)
		{
			var ret = new ColumnHeaderEx() { Text = text };
			this.Add(ret);
			return ret;
		}

		public ColumnHeaderEx Add(string text, int width)
		{
			var hdr = new ColumnHeaderEx() { Text = text, Width = width };
			list.Add(hdr);
			return hdr;
		}

		public ColumnHeaderEx Add(string key, string text)
		{
			var hdr = new ColumnHeaderEx() { Text = text, Name = key };
			list.Add(hdr);
			return hdr;
		}

		public ColumnHeaderEx Add(string text, int width, HorizontalAlignment textAlign)
		{
			var hdr = new ColumnHeaderEx() { Text = text, Width = width, TextAlign = textAlign };
			list.Add(hdr);
			return hdr;
		}

		public ColumnHeaderEx Add(string key, string text, int width)
		{
			var hdr = new ColumnHeaderEx() { Text = text, Name = key, Width = width };
			list.Add(hdr);
			return hdr;
		}

		public ColumnHeaderEx Add(string key, string text, int width, HorizontalAlignment textAlign, int imageIndex)
		{
			var hdr = new ColumnHeaderEx() { Text = text, Name = key, Width = width, TextAlign = textAlign, ImageIndex = imageIndex };
			list.Add(hdr);
			return hdr;
		}

		public ColumnHeaderEx Add(string key, string text, int width, HorizontalAlignment textAlign, string imageKey)
		{
			var hdr = new ColumnHeaderEx() { Text = text, Name = key, Width = width, TextAlign = textAlign, ImageKey = imageKey };
			list.Add(hdr);
			return hdr;
		}

		public void AddRange(ColumnHeaderEx[] values)
		{
			this.AddRange(values as IEnumerable<ColumnHeaderEx>);
		}

		public void AddRange(IEnumerable<ColumnHeaderEx> items)
		{
			this.listView.BeginUpdate();
			foreach (var item in items)
				this.Add(item);
			this.listView.EndUpdate();
		}

		public void AddRange(IEnumerable<string> items)
		{
			this.listView.BeginUpdate();
			foreach (var item in items)
				this.Add(item);
			this.listView.EndUpdate();
		}

		public void Clear()
		{
			list.Clear();
		}

		public bool Contains(ColumnHeaderEx item)
		{
			return list.Contains(item);
		}

		public bool ContainsKey(string key)
		{
			return list.ContainsKey(key);
		}

		public void CopyTo(ColumnHeaderEx[] array, int arrayIndex)
		{
			if (list.Count + arrayIndex > array.Length)
				throw new ArgumentOutOfRangeException();
			for (int i = 0; i < list.Count; i++)
				array[i + arrayIndex] = list[i] as ColumnHeaderEx;
		}

		public System.Collections.Generic.IEnumerator<ColumnHeaderEx> GetEnumerator()
		{
			return list.Cast<ColumnHeaderEx>().GetEnumerator();
		}

		void ICollection.CopyTo(Array array, int index)
		{
			if (this.Count > 0)
				Array.Copy(this.ToArray(), 0, array, index, this.Count);
		}

		void ICollection<ColumnHeaderEx>.Add(ColumnHeaderEx item)
		{
			list.Add(item);
		}

		bool ICollection<ColumnHeaderEx>.Remove(ColumnHeaderEx item)
		{
			int idx = this.IndexOf(item);
			if (idx == -1)
				return false;
			list.RemoveAt(idx);
			return true;
		}

		int IList.Add(object value)
		{
			if (!(value is ColumnHeaderEx))
				throw new ArgumentException();
			return this.Add((ColumnHeaderEx)value);
		}

		bool IList.Contains(object value)
		{
			if (!(value is ColumnHeaderEx))
				throw new ArgumentException();
			return this.Contains((ColumnHeaderEx)value);
		}

		int IList.IndexOf(object value)
		{
			if (!(value is ColumnHeaderEx))
				throw new ArgumentException();
			return this.IndexOf((ColumnHeaderEx)value);
		}

		void IList.Insert(int index, object value)
		{
			if (!(value is ColumnHeaderEx))
				throw new ArgumentException();
			this.Insert(index, (ColumnHeaderEx)value);
		}

		void IList.Remove(object value)
		{
			if (!(value is ColumnHeaderEx))
				throw new ArgumentException();
			this.Remove((ColumnHeaderEx)value);
		}

		public int IndexOf(ColumnHeaderEx item)
		{
			return list.IndexOf(item);
		}

		public int IndexOfKey(string key)
		{
			return list.IndexOfKey(key);
		}

		public void Insert(int index, ColumnHeaderEx item)
		{
			list.Insert(index, item);
		}

		public void Insert(int index, string text)
		{
			var hdr = new ColumnHeaderEx() { Text = text };
			this.Insert(index, hdr);
		}

		public void Insert(int index, string text, int width)
		{
			var hdr = new ColumnHeaderEx() { Text = text, Width = width };
			this.Insert(index, hdr);
		}

		public void Insert(int index, string key, string text)
		{
			var hdr = new ColumnHeaderEx() { Text = text, Name = key };
			this.Insert(index, hdr);
		}

		public void Insert(int index, string text, int width, HorizontalAlignment textAlign)
		{
			var hdr = new ColumnHeaderEx() { Text = text, Width = width, TextAlign = textAlign };
			this.Insert(index, hdr);
		}

		public void Insert(int index, string key, string text, int width)
		{
			var hdr = new ColumnHeaderEx() { Text = text, Name = key, Width = width };
			this.Insert(index, hdr);
		}

		public void Insert(int index, string key, string text, int width, HorizontalAlignment textAlign, int imageIndex)
		{
			var hdr = new ColumnHeaderEx() { Text = text, Name = key, Width = width, TextAlign = textAlign, ImageIndex = imageIndex };
			this.Insert(index, hdr);
		}

		public void Insert(int index, string key, string text, int width, HorizontalAlignment textAlign, string imageKey)
		{
			var hdr = new ColumnHeaderEx() { Text = text, Name = key, Width = width, TextAlign = textAlign, ImageKey = imageKey };
			this.Insert(index, hdr);
		}

		public void Remove(ColumnHeaderEx item)
		{
			list.Remove((ColumnHeader)item);
		}

		public void RemoveAt(int index)
		{
			list.RemoveAt(index);
		}

		public void RemoveByKey(string key)
		{
			list.RemoveByKey(key);
		}

		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
		{
			return this.GetEnumerator();
		}

		Xml.Schema.XmlSchema IXmlSerializable.GetSchema()
		{
			return null;
		}

		void IXmlSerializable.ReadXml(Xml.XmlReader reader)
		{
			reader.MoveToContent();
			int count = Convert.ToInt32(reader.GetAttribute("Count"));
			if (count == this.Count)
			{
				for (int i = 0; i < count; i++)
				{
					reader.ReadStartElement("Column");
					string attr = reader.GetAttribute("DisplayIndex");
					if (!string.IsNullOrEmpty(attr))
						this[i].DisplayIndex = Convert.ToInt32(attr);
					attr = reader.GetAttribute("Width");
					if (!string.IsNullOrEmpty(attr))
						this[i].Width = Convert.ToInt32(attr);
					attr = reader.GetAttribute("SortOrder");
					if (!string.IsNullOrEmpty(attr))
						this[i].SortOrder = (SortOrder)Enum.Parse(typeof(SortOrder), attr);
					reader.ReadEndElement();
				}
			}
		}

		void IXmlSerializable.WriteXml(Xml.XmlWriter writer)
		{
			writer.WriteAttributeString("Count", this.Count.ToString());
			for (int i = 0; i < this.Count; i++)
			{
				writer.WriteStartElement("Column");
				writer.WriteAttributeString("Index", i.ToString());
				if (this[i].DisplayIndex != -1)
					writer.WriteAttributeString("DisplayIndex", this[i].DisplayIndex.ToString());
				writer.WriteAttributeString("Width", this[i].Width.ToString());
				if (this[i].SortOrder != SortOrder.None)
					writer.WriteAttributeString("SortOrder", this[i].SortOrder.ToString());
				writer.WriteEndElement();
			}
		}
	}

	internal class ColumnHeaderExCollectionEditor : CollectionEditor
	{
		public ColumnHeaderExCollectionEditor(Type type) : base(type)
		{
		}

		protected override string HelpTopic
		{
			get { return "net.ComponentModel.ColumnHeaderCollectionEditor"; }
		}

		protected override Type CreateCollectionItemType()
		{
			return typeof(ColumnHeaderEx);
		}

		/*internal override void OnItemRemoving(object item)
		{
			ListView instance = base.Context.Instance as ListView;
			if (instance != null)
			{
				ColumnHeaderEx column = item as ColumnHeaderEx;
				if (column != null)
				{
					IComponentChangeService service = base.GetService(typeof(IComponentChangeService)) as IComponentChangeService;
					PropertyDescriptor member = null;
					if (service != null)
					{
						member = TypeDescriptor.GetProperties(base.Context.Instance)["Columns"];
						service.OnComponentChanging(base.Context.Instance, member);
					}
					instance.Columns.Remove(column);
					if ((service != null) && (member != null))
					{
						service.OnComponentChanged(base.Context.Instance, member, null, null);
					}
				}
			}
		}*/

		protected override object SetItems(object editValue, object[] value)
		{
			if (editValue != null)
			{
				ColumnHeaderExCollection headers = editValue as ColumnHeaderExCollection;
				if (headers != null)
				{
					headers.Clear();
					ColumnHeaderEx[] destinationArray = new ColumnHeaderEx[value.Length];
					Array.Copy(value, 0, destinationArray, 0, value.Length);
					headers.AddRange(destinationArray);
				}
			}
			return editValue;
		}
	}
}